package jeons.admin.product.biz.impl;

import java.util.Iterator;
import java.util.List;

import jeons.admin.product.biz.CaseBiz;
import jeons.admin.product.biz.ProductBiz;
import jeons.admin.product.biz.ProductImgBiz;
import jeons.admin.product.dto.CaseDTO;
import jeons.admin.product.dto.ProductDTO;
import jeons.admin.product.dto.ProductImgDTO;
import jeons.common.util.FileService;
import jeons.common.util.Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Service
@Transactional

public class CaseBizImpl extends SqlMapClientDaoSupport implements CaseBiz{
	@Autowired SqlMapClientTemplate sqlMapClientTemplate;
	@Autowired FileService fileService;
	@Autowired ProductImgBiz productImgBiz;
	@Autowired ProductBiz productBiz;
	
	/**
	 * <PRE>
	 * 1. MethodName : insertData
	 * 2. ClassName  : CaseBizImpl
	 * 3. Comment   :	
	 * 4. �ۼ���    : Administrator
	 * 5. �ۼ���    : 2012. 1. 21. ���� 9:53:26
	 * </PRE>
	 *   @param caseDTO
	 *   @param listimg
	 *   @param dteilimg
	 *   @return
	 */
	@Override
	public CaseDTO insertData(CaseDTO caseDTO, List listimg, List dteilimg) {
		ProductDTO productDTO = new ProductDTO();
		productDTO.setPrice(caseDTO.getPrice());
		productDTO.setType(caseDTO.getType());
		productDTO.setP_number(caseDTO.getP_number());
		productDTO.setProduct(caseDTO.getProduct());
		productDTO.setRgdate(caseDTO.getRgdate());
		productDTO.setManufacturer(caseDTO.getManufacturer());
		
		sqlMapClientTemplate.insert("Product.insertData",productDTO);
		caseDTO.setProno(productDTO.getProno());
		sqlMapClientTemplate.insert("Case.insertData",caseDTO);
		
		String today = Utils.getYmdhhmmss("yyyyMMddhhmmss");
		ProductImgDTO productImgDTO = new ProductImgDTO();
		for (Iterator iterator = dteilimg.iterator();  iterator.hasNext();) {
			MultipartFile multipartFile = (MultipartFile)iterator.next();
			fileService.uploadData(multipartFile,"product",today);
			productImgDTO.setProno(productDTO.getProno());		// ��ǰ���̺� �ڵ�
			productImgDTO.setType(productDTO.getType());
			productImgDTO.setB_filename(today+multipartFile.getOriginalFilename());
			productImgDTO.setB_realname(multipartFile.getOriginalFilename());
			productImgDTO.setB_filesize((int)multipartFile.getSize());
			//System.out.println("listimg :"+multipartFile.getOriginalFilename());;
			
			
		}	//����
		for (Iterator iterator =  listimg.iterator();  iterator.hasNext();) {
			MultipartFile multipartFile = (MultipartFile)iterator.next();
			fileService.uploadData(multipartFile,"product",today);
			productImgDTO.setL_filename(today+multipartFile.getOriginalFilename());
			productImgDTO.setL_realname(multipartFile.getOriginalFilename());
			productImgDTO.setL_filesize((int)multipartFile.getSize());
			
		}
		productImgBiz.uploadImgData(productImgDTO);
		return caseDTO;
	}

	@Override
	public CaseDTO getData(int prono) {
		return (CaseDTO) sqlMapClientTemplate.queryForObject("Case.getData",prono);
	}

	/* (non-Javadoc) 업데이트
	 * @see jeons.admin.product.biz.CaseBiz#updateData(jeons.admin.product.dto.CaseDTO)
	 */
	@Override
	public void updateData(CaseDTO caseDTO) {
		ProductDTO productDTO = new ProductDTO();
		productDTO.setPrice(caseDTO.getPrice());
		productDTO.setType(caseDTO.getType());
		productDTO.setP_number(caseDTO.getP_number());
		productDTO.setProduct(caseDTO.getProduct());
		productDTO.setRgdate(caseDTO.getRgdate());
		productDTO.setManufacturer(caseDTO.getManufacturer());
		productDTO.setProno(caseDTO.getProno());
		productBiz.updateData(productDTO);
		caseDTO.setProno(productDTO.getProno());
		sqlMapClientTemplate.update("Case.updateDataI",caseDTO);
	}

	@Override
	public CaseDTO getALLData(int prono) {
		return (CaseDTO) sqlMapClientTemplate.queryForObject("Case.getALLData",prono);
	}

}
